
<template>
  <div class="register" id="col">
    <van-nav-bar title="注册" right-text="登录" @click-right="onClickRight" />
    <h1 class="logo">
      <img src="https://file.sdyxmall.com/h5/auth/public/app/img/logo.adec77d.png" alt />
    </h1>

    <van-field v-model="data.phone" center clearable placeholder="手机号码">
      <van-button
        slot="button"
        size="small"
        color="linear-gradient(to right, #be3335, #f40)"
        class="btn"
        @click="getSmsCode"
      >发送验证码</van-button>
    </van-field>
    <van-field v-model="data.smsCode" placeholder="验证码" />
    <van-field v-model="data.password" type="password" placeholder="密码" right-icon="closed-eye" />

    <van-button color="linear-gradient(to right, #be3335, #f40)" class="dx-login" @click="submit">注册</van-button>
    <div class="agreement-text">
      <van-checkbox
        v-model="checked"
        checked-color="#f40"
        @change="select"
        style="margin-right:5px;margin-left:100px;margin-top:5px;"
      ></van-checkbox>注册即代表同意
      <span class="agreement-link">《服务条款》</span>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "register",
  data() {
    return {
      data: {
        phone: "",
        smsCode: "",
        password: ""
      },
      checked: false
    };
  },
  methods: {
    getSmsCode() {
      let smscode = /^1[3-9]\d{9}/;
      let flag = smscode.test(this.data.phone);
      if (flag) {
        let data = this.data;
        this.Toast(() => {
          let url = "/user/getSmsCode";
          this.$axios
            .post(url, data)
            .then(res => {
              console.log("res", res);
            })
            .catch(err => {
              this.$fail(err);
            });
        });
      } else {
        this.$toast("请输入正确的手机号码");
      }
    },
    select(value) {
      this.checked = value;
    },
    submit() {
      let smscode = /^1[3-9]\d{9}/;
      let flag = smscode.test(this.data.phone);
      if (flag) {
        if (this.checked) {
          let data = this.data;
          let url = "/user/register";
          this.$axios
            .post(url, data)
            .then(res => {
              this.$toast.success("注册成功");
              setTimeout(() => {
                this.$router.push("/login");
              }, 1000);
            })
            .catch(err => {
              this.$fail(err);
            });
        } else {
          this.$toast("未勾选服务条款");
        }
      } else {
        this.$toast("请输入正确的手机号码");
      }
    },
    onClickRight() {
      this.$router.push("/login");
    },
    Toast(callback) {
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "验证码发送中"
      });

      let second = 3;
      const timer = setInterval(() => {
        second--;
        if (second) {
          toast.message = `发送中 ${second} 秒`;
        } else {
          clearInterval(timer);
          // 手动清除 Toast
          Toast.clear();
          callback();
        }
      }, 1000);
    }
  }
};
</script>
<style lang="less">
.van-nav-bar__text {
  color: #191a1b;
  font-size: 15px;
}
.van-cell {
  color: #2c3e50;
  width: 325px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  border: none;
  outline: none;
}
.van-nav-bar {
  height: 44px;
}
</style>
<style lang="less" scoped>
.logo {
  text-align: center;
  margin: 50px 0;
  img {
    width: 140px;
    height: 26px;
  }
  background: #fff;
}
.dx-login {
  margin-top: 42px;
  width: 325px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  background: #c03131;
}
.btn {
  background: none;
  border: none;
  color: #bdc0c5;
}
.agreement-text {
  display: flex;
  height: 30px;
  line-height: 30px;
  margin-top: 10px;
  text-align: center;
  font-size: 11px;
  color: #797d82;
  .agreement-link {
    color: #191a1b;
  }
}
#col {
  height: 100vh;
  background: #fff;
  width: 100%;
}
</style>